﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Auditing;
using Volo.Abp.AuditLogging;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Platform
{
    /// <summary>
    /// 审计日志
    /// </summary>
    public class AuditLogService : PlatformAppService
    {
        private readonly IAuditLogRepository _auditLogRepository;

        public AuditLogService(IAuditLogRepository auditLogRepository)
        {
            _auditLogRepository = auditLogRepository;
        }

        public async Task<PagedResultDto<AuditLogDto>> GetListAsync(AuditLogInput input)
        {
            var skipCount = (input.Pi - 1) * input.Ps;
            var maxResultCount = input.Ps;
            input.StartTime = input.StartTime.HasValue ? input.StartTime.Value.Date : DateTime.Now.Date;
            input.EndTime = input.EndTime.HasValue ? input.EndTime.Value.Date.AddDays(1).AddSeconds(-1) : DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            var list = await _auditLogRepository.GetListAsync(startTime: input.StartTime, endTime: input.EndTime
                , maxResultCount: maxResultCount, skipCount: skipCount, includeDetails: true);
            var count = await _auditLogRepository.GetCountAsync(startTime: input.StartTime, endTime: input.EndTime);
            var itemsDto = ObjectMapper.Map<List<AuditLog>, List<AuditLogDto>>(list);

            return new PagedResultDto<AuditLogDto>(count, itemsDto);
        }
    }
}
